package com.starbaba.system.translate.view;

import java.io.Serializable;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.Action;
import com.starbaba.core.utils.PkUtils;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.core.web.struts2.JsonResult;
import com.starbaba.core.web.translate.TranslateUtilsFactory;
import com.starbaba.core.web.translate.TranslateVO;

@SuppressWarnings("serial")
public class JsonUtilAction extends BaseAction {
	
	public void getListJson(){
		JsonResult result = new JsonResult();
		try{
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);			
			String definition = (String) paramsMap.get("definition");
			if(StringUtils.isNotBlank(definition)){
				result.setStatus(Action.SUCCESS);
				List<TranslateVO> translateList= TranslateUtilsFactory.getInstance().getCollection(definition);
				result.setMessage(JSONArray.fromObject(translateList).toString());
			}
		}catch(Exception ex){
			result.setStatus(Action.ERROR);
			result.setMessage("异常结束,异常信息："  + ex.getMessage());
		}
		
		try{
			this.sendJsonMessage(result.getJsonString());
		}catch(Exception ex){
		}
	}
	
	public void getNameJson(){
		JsonResult result = new JsonResult();
		try{
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);			
			String definition = (String) paramsMap.get("definition");
			String value = (String) paramsMap.get("value");
			if(StringUtils.isNotBlank(definition)){
				result.setStatus(Action.SUCCESS);
				String name = TranslateUtilsFactory.getInstance().getName(definition, value);
				result.setMessage(name);
			}
		}catch(Exception ex){
			result.setStatus(Action.ERROR);
			result.setMessage("异常结束,异常信息："  + ex.getMessage());
		}
		try{
			this.sendJsonMessage(result.getJsonString());
		}catch(Exception ex){
		}
	}
	
	public void getVOJson() throws Exception{
		JsonResult result = new JsonResult();
		try{
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
			String voName = (String)paramsMap.get("voName");
			String pkName = (String)paramsMap.get("pkName");
			String pkValue = (String) paramsMap.get("pkValue");
			if(StringUtils.isBlank(pkValue)){
				result.setStatus(Action.ERROR);
				result.setMessage("pkValue为空.");
				this.sendJsonMessage(result);
				return;
			}
			pkValue = new String(pkValue.getBytes("ISO8859-1"), "utf-8");
			try{
				this.voClass = Class.forName(voName);
			}catch(Exception ex){
			}
			if(null == voClass){
				result.setStatus(Action.ERROR);
				result.setMessage("voName 定义有误.");
				this.sendJsonMessage(result);
				return;
			}
			Serializable pk = null;
			try{
				pk = PkUtils.getPkVO(voClass, pkName, pkValue);
			}catch(Exception ex){
			}
			if(null == pk){
				result.setStatus(Action.ERROR);
				result.setMessage("主键定义有误.");
				this.sendJsonMessage(result);
				return;
			}
			try{
				this.entityVO = this.getService().doFindByPk(voClass, pk);
			}catch(Exception ex){
			}
			if(null == entityVO){
				result.setStatus(Action.ERROR);
				result.setMessage("记录不存在.");
				this.sendJsonMessage(result);
				return;
			}
			result.setStatus(Action.SUCCESS);
			this.setTargetObjectToParamsMap(this.getEntityVO(), paramsMap);
			result.setMessage(JSONObject.fromObject(paramsMap).toString());
			this.sendJsonMessage(result);
		}catch(Exception ex){
			result.setStatus(Action.ERROR);
			result.setMessage("异常结束,异常信息："  + ex.getMessage());
			this.sendJsonMessage(result);
		}
	}

}
